<?php
/**
 * 业务异常
 * Author: lee wei
 * Date: 2021/12/8
 */
namespace app\common\exception;

//use app\common\JsonResponse;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\exception\ClassNotFoundException;
use think\exception\FuncNotFoundException;
use think\exception\RouteNotFoundException;
use think\Response;
use Throwable;

class BusinessException extends Handle
{
    //use JsonResponse;
    private $code;
    private $msg;
    private $errorCode;

    public function render($request, Throwable $e): Response
    {

        // 参数验证错误
        if ($e instanceof BusinessException) {
            return json($e->getError(), 422);
        }

        // 请求异常
        if ($e instanceof HttpException && $request->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }

}